<template>
  <div class="footer">
    <div class="disable" v-if="!isShow">
      <!-- 这里是未激活状态 -->
      <input
        type="text"
        placeholder="写跟帖"
        @focus="showTextarea"
        :value="content"
      />

      <span> <van-icon name="chat-o" badge="99+" /></span>
      <span><van-icon name="star-o" /></span>
      <span><van-icon name="share-o" /></span>
    </div>
    <div class="enable" v-if="isShow">
      <!-- 这里是已激活状态 -->
      <!-- @blur 当元素失去焦点时所触发的事件 -->
      <textarea
        rows="3"
        @blur="hideTextarea"
        ref="textarea"
        v-model="content"
      ></textarea>
      <div class="btnSubmit" @click="send">发送</div>
    </div>
  </div>
</template>

<script>
import evenBus from "../../utils/eventBus";
export default {
  data() {
    return {
      isShow: false,
      content: "",
      commentId: "",
    };
  },
  created() {
    evenBus.$off("callReply");
    evenBus.$on("callReply", (commentId) => {
      console.log("输入框知道了评论被点击了");
      this.commentId = commentId;
      this.showTextarea();
    });
  },
  methods: {
    send() {
      this.$axios({
        method: "post",
        url: "/post_comment/" + this.$route.query.id,
        data: {
          content: this.content,
          parent_id: this.commentId,
        },
      }).then((res) => {
        console.log(res.data);
        if (res.data.message == "评论发布成功") {
          this.$emit("reloadComment");
          this.content = "";
        }
      });
    },
    showTextarea() {
      this.isShow = true;
      // 只有最后一次生效
      // 等待渲染, 最简单的方案是定时器
      // 随便猜测一个时间
      // setTimeout(() => {
      //     this.$refs.textarea.focus();
      // }, 50);
      // vue 提供了一个函数 叫做 nextTick
      // 可以接受一个回调函数作为参数, 这个回调中的
      // 代码都会等待下一次渲染时再执行
      this.$nextTick(() => {
        this.$refs.textarea.focus();
      });
    },
    hideTextarea() {
      setTimeout(() => {
        this.commentId = "";
        this.isShow = false;
      }, 100);
    },
  },
};
</script>

<style lang="less" scoped>
.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #f2f2f2;
  .disable {
    display: flex;
    justify-content: space-around;
    align-items: center;
    input {
      background-color: #d7d7d7;
      outline: none;
      border: 0;
      padding: 0 16/360 * 100vw;
      width: 176/360 * 100vw;
      box-sizing: border-box;
      line-height: 28/360 * 100vw;
      border-radius: 15/360 * 100vw;
      font-size: 14/360 * 100vw;
    }
    span {
      font-size: 25/360 * 100vw;
      padding-top: 10/360 * 100vw;
    }
  }
  .enable {
    margin-top: 20/360 * 100vw;
    margin-bottom: 10/360 * 100vw;
    display: flex;
    align-items: flex-end;
    justify-content: space-around;
    textarea {
      width: 200/360 * 100vw;
      height: 50/360 * 100vw;
      background-color: #d7d7d7;
      outline: none;
      border: 0;
      border-radius: 13/360 * 100vw;
      padding: 20/360 * 100vw;
    }
    .btnSubmit {
      color: #fff;
      border: 1px solid #ff0000;
      background-color: #ff0000;
      padding: 5/360 * 100vw 15/360 * 100vw;
      font-size: 12/360 * 100vw;
      border-radius: 20/360 * 100vw;
      margin-right: 20/360 * 100vw;
    }
  }
}
</style>